package cn.nexd.map.rendering.core.componet;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Picture;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewTreeObserver;

import java.util.List;
import java.util.Map;

import cn.nexd.map.rendering.core.SVGMapBaseOverlay;
import cn.nexd.map.rendering.core.componet.data.Feature;
import cn.nexd.map.rendering.core.componet.data.FloorsFeature;
import cn.nexd.map.rendering.core.helper.RectHelper;


final class MapOverlay extends SVGMapBaseOverlay {
    private MapMainView mapMainView;
    private FloorsFeature floorsFeature;
    private Context context;
    private Picture floorMap;
    private Paint paint;
    private boolean hasMeasured;

    private Feature feature;

    private static final String TAG = "MapLayer";
    private MapMainView.MapViewClickListener mapViewClickListener;
    private MapMainView.MapViewOtherClickListener mapViewOtherClickListener;

    public MapOverlay(MapMainView mapMainView, FloorsFeature floorsFeature, Context context) {
        this.mapMainView = mapMainView;
        this.floorsFeature = floorsFeature;
        this.context = context;
        this.paint = new Paint();
        this.showLevel = MAP_LEVEL;
    }

    public void setData(Picture floorMap) {
        this.floorMap = floorMap;
        if (this.mapMainView.getWidth() == 0) {
            ViewTreeObserver vto = this.mapMainView.getViewTreeObserver();
            vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                public boolean onPreDraw() {
                    if (!hasMeasured) {
                        calcRatio();
                    }
                    return true;
                }
            });
        } else {
            calcRatio();
        }
    }

    public Picture getFloorMap() {
        return this.floorMap;
    }

    public void registerMapViewClickListener(MapMainView.MapViewClickListener mapViewClickListener) {
        this.mapViewClickListener = mapViewClickListener;
    }

    public void registerMapViewOtherClickListener(MapMainView.MapViewOtherClickListener mapViewOtherClickListener) {
        this.mapViewOtherClickListener = mapViewOtherClickListener;
    }

    private void calcRatio() {
        float zoom = getInitScale(this.mapMainView.getWidth(), this.mapMainView.getHeight(), floorMap.getWidth(), floorMap.getHeight());
        Log.i(TAG, zoom + " = zoom");
        this.mapMainView.setMinZoomValue(zoom);
        this.mapMainView.setCurrentZoomValue(zoom, 0, 0);
        float deltaHeight = this.mapMainView.getHeight() - zoom * floorMap.getHeight();
        float deltaWidth = this.mapMainView.getWidth() - zoom * floorMap.getWidth();
        this.mapMainView.translateBy(deltaWidth / 2, deltaHeight / 2);
        this.mapMainView.refresh();
        hasMeasured = true;
    }

    private float getInitScale(float width, float height, float imageWidth, float imageHeight) {
        float widthRatio = width / imageWidth;
        float heightRatio = height / imageHeight;
        if (widthRatio * imageHeight <= height) {
            return widthRatio;
        } else if (heightRatio * imageWidth <= width) {
            return heightRatio;
        }
        return 0;
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onResume() {
    }

    @Override
    public void onTap(MotionEvent event) {

        final float map_coordinatie[] = mapMainView.getMapCoordinateWithScreenCoordinate(event.getX(), event.getY());

//        Log.d("onTap", " \n" + " \nonTap_x == " + event.getX() + " \nonTap_y == " + event.getY() + " \nmapMainView_x == " + mapMainView.getX() + " \nmapMainView_y == " + mapMainView.getY()
//                + " \nmapMainView_width == " + mapMainView.getWidth()
//                + " \nmapMainView_height == " + mapMainView.getHeight()
//                + " \nmapCoordinateWithScreenCoordinate_x == " + map_coordinatie[0]
//                + " \nmapCoordinateWithScreenCoordinate_y == " + map_coordinatie[1]
//                + " " + floorsFeature.toString());
        float map_coordinatie_correctly_x = map_coordinatie[0];
        float map_coordinatie_correctly_y = map_coordinatie[1];

        if ((map_coordinatie_correctly_x >= floorsFeature.getMinX() && map_coordinatie_correctly_x <= floorsFeature.getMaxX()) && (map_coordinatie_correctly_y >= floorsFeature.getMinY() && map_coordinatie_correctly_y <= floorsFeature.getMaxY())) {
//            Log.d("onTap", "点击点在地图范围内");
            for (int i = 0; i < floorsFeature.getStrokeNodes().size(); i++) {
                boolean inMap = RectHelper.insideJudger(floorsFeature.getStrokeNodes().get(i), map_coordinatie_correctly_x, map_coordinatie_correctly_y, 0, 0);
                if (inMap) {
//                    Log.d("onTap", "点击点在地图范围内");
                    List<Map<Long, Feature>> lists = floorsFeature.getFeatures();

                    int count = 0;
                    A:
                    for (int a = 0, len = lists.size(); a < len; a++) {
                        Map<Long, Feature> featureMap = floorsFeature.getFeatures().get(a);
                        for (Map.Entry entry : featureMap.entrySet()) {
                            long id = (long) entry.getKey();
                            Feature feature = (Feature) entry.getValue();
//                            List<Node> nodes = feature.getNodes();
                            boolean insideJudger = RectHelper.insideJudger(feature.getNodes(), map_coordinatie_correctly_x, map_coordinatie_correctly_y, floorsFeature.getOffset_x(), floorsFeature.getOffset_y());
                            if (insideJudger) {
//                                Log.d("onTap", "点击点所在的区域：" + feature.toString());
                                count++;
////                                Toast.makeText(mapMainView.getContext(), "点击点所在的区域 ID == " + feature.getFeature_id() + "name == " + feature.getName(), Toast.LENGTH_LONG).show();
//                                if (mapViewClickListener != null) {
//                                    mapViewClickListener.onClick(event.getX(), event.getY(), map_coordinatie_correctly_x, map_coordinatie_correctly_y, feature, floorsFeature);
//                                }
                                this.feature = feature;
                                break A;
                            } /*else {
                                count = 2;
//                                Log.d("onTap", "点击了非feature区域" + feature.toString());
//                                if (mapViewOtherClickListener != null) {
//                                    mapViewOtherClickListener.onClick(event.getX(), event.getY(), map_coordinatie_correctly_x, map_coordinatie_correctly_y);
//                                }
                            }*/
                        }
                    }

                    if (count == 1) {
                        Log.d("onTap", "点击点所在的区域：" + this.feature.toString());
                        if (mapViewClickListener != null) {
                            mapViewClickListener.onClick(event.getX(), event.getY(), map_coordinatie_correctly_x, map_coordinatie_correctly_y, feature, floorsFeature);
                        }
                        //////////高亮///////////
                    } else {
                        Log.d("onTap", "点击了非feature区域");
                        if (mapViewOtherClickListener != null) {
                            mapViewOtherClickListener.onClick(event.getX(), event.getY(), map_coordinatie_correctly_x, map_coordinatie_correctly_y);
                        }
                    }

                }
            }
        } else {
            Log.d("onTap", "点击点不在地图范围内");

        }

    }

    @Override
    public void onDestroy() {
//        this.floorMap = null;
    }

    @Override
    public void draw(Canvas canvas, Matrix matrix, float currentZoom, float currentRotateDegrees) {
        canvas.save();
        canvas.setMatrix(matrix);

        if (floorMap != null) {
            canvas.drawPicture(floorMap);
        }


//        for (int i = 0; i < floorsFeature.getFeatures().size(); i++) {
//            Map<Long, Feature> map = floorsFeature.getFeatures().get(i);
//            for (Map.Entry<Long, Feature> entry : map.entrySet()) {
//                Feature feature = entry.getValue();
//                /////////
//                String spaceTypeString = entry.getValue().getSpaceTypeString();
//
//                float center_x = feature.getCenter_x();
//                float center_y = feature.getCenter_y();
//                float gol[] = {center_x, center_y};
//                matrix.mapPoints(gol);
//                Bitmap bitmap = null;
//                canvas.save();
//                if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "01")) {
//                    // 会议室
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_meetingroom.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_meetingroom.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "02")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_toilet.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_toilet.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "04")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_elevator.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_elevator.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "05")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_stairs.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_stairs.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "07")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_parking.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_parking.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                } else if (!TextUtils.isEmpty(spaceTypeString) && TextUtils.equals(spaceTypeString, "17")) {
////                        bitmap = BitmapUtils.small(context.getAssets(), "image/icon_diningroom.png");
//                    bitmap = BitmapUtils.decodeSampleBitmapFromAssets(context.getAssets(), "image/icon_diningroom.png");
//                    canvas.drawBitmap(bitmap, gol[0], gol[1], new Paint());
//                }
//                canvas.restore();
//            }
//        }
        canvas.restore();

    }
}
